$(document).ready(function () {
    $('#register-username').on('input', function () {
        if ($(this).val()) {
            $('#register-username-del').attr('style', 'display: block;');
        } else {
            $('#register-username-del').attr('style', 'display: none;');
        }
    });
    $('#register-password').on('input', function () {
        if ($(this).val()) {
            $('#register-password-del').attr('style', 'display: block;');
        } else {
            $('#register-password-del').attr('style', 'display: none;');
        }
    });
    $('#register-confirmpassword').on('input', function () {
        if ($(this).val()) {
            $('#register-confirmpassword').attr('style', 'display: block;');
        } else {
            $('#register-confirmpassword').attr('style', 'display: none;');
        }
    });
    $('#register-username-del').click(function () {
        $('#register-username').val(null);
        $(this).attr('style', 'display: none;');
    });
    $('#register-password-del').click(function () {
        $('#register-password').val(null);
        $(this).attr('style', 'display: none;');
    });
    $('#register-confirmpassword-del').click(function () {
        $('#register-confirmpassword-del').val(null);
        $(this).attr('style', 'display: none;');
    });
    $(document).keydown(function (event_e) {
        if (window.event) {
            event_e = window.event;
        }
        var int_keycode = event_e.charCode || event_e.keyCode;
        if (int_keycode == 13) {
            $('#register-btn').click();
        }
    });
    var isCompany = false;
    // 所在部门下拉监听
    layui.form.on('select(register-select-role)', function (data) {
        $.get('/cms/sysManage/role/'+data.value, function (data) {
            console.log(data);
            if (data.data.roleName.indexOf('企业') != -1) {
                isCompany = true;
                $("#register-company-select").show();
            }else{
                isCompany = false;
                $("#register-company-select").hide();
            }
        });

    });

    cmsUtils.renderDynamicSelectionBox('/cms/sysManage/getRoles', 'register-select-role', 'id', 'name');
    cmsUtils.renderDynamicSelectionBox('/inviteCompany/listSelect', 'register-company', 'code', 'value');
    layui.form.render('select');
    $('#register-btn').click(function () {
        var username = $('#register-username');
        var password = $('#register-password');
        var confirmpassword = $('#register-confirmpassword');
        var roleId = $('#register-select-role').val();
        var companyId = $('#register-company').val();
        console.log(roleId);
        if (isCompany && !companyId) {
            layer.tips('请选择公司，若没有，请联系管理员', username);
            return;
        }
        if (!username.val()) {
            layer.tips('请填写用户名', username);
            return;
        }
        if (!password.val()) {
            layer.tips('请填写密码', password);
            return;
        }
        if (password.val().length < 4) {
            layer.msg('密码长度为4~20位', {anim: 6});
            return ;
        }
        if (password.val() != confirmpassword.val()) {
            layer.msg('两次新密码输入不一致', {anim: 6});
            return ;
        }
        if (!roleId) {
            layer.msg('请选择角色', {anim: 6});
            return ;
        }

        var index = layer.msg('正在注册，请稍后...', {
            offset: [(username.offset().top+20), (username.offset().left+70)],
            shade: [0.2, '#393D49'],
            time: 0
        });
        $.ajax({
            type: 'post',
            url: '/cms/register',
            data:{
                'username': username.val(),
                'roleId': roleId,
                'companyId': companyId,
                'password': password.val()
                // 'password': md5(password.val())
            },
            dataType: 'json',
            success: function (data) {
                layer.close(index);
                if (data.code == 200) {
                    location.href = '/index.html';
                } else {
                    layer.msg(data.msg, {
                        offset: [(username.offset().top+20), (username.offset().left+90)],
                        anim: 6
                    });
                }
            },
            error: function () {
                layer.close(index);
                layer.msg('error！', {
                    offset: [(username.offset().top+20), (username.offset().left+90)],
                    anim: 6
                });
            }
        });
    });
});